/**
 * Created by Administrator on 2017/8/30.
 */

$(document).ready(function () {
    //详情和评价切换
    $('.goodsdetail-sw-rl').click(function () {
        $(this).addClass('goodsdetail-active1').find('.ab').addClass('goodsdetail-active');
        $(this).siblings().removeClass('goodsdetail-active1').find('.ab').removeClass('goodsdetail-active');
        var index = $(this).index();
        $(".goodsdetail-con > div").eq(index).show().siblings().hide();
    });

    var detailTpl=$('#detail-template').html();
    var detailCmp=Handlebars.compile(detailTpl);

    //商品sku
    var goodsTypeTpl=$('#goods-type-template').html();
    var goodsTypeCmp=Handlebars.compile(goodsTypeTpl);

    //详情图片模板
    var detailImgTpl=$('#goodsdetail-con-detail').html();
    var detailImgCmp=Handlebars.compile(detailImgTpl);

    //获取款式
    var oneSkuId=null;

       //全部评价，带图评价选项卡
    var comWholeTpl=$('#goodsdetail-con-whole').html();
    var comWholeCmp=Handlebars.compile(comWholeTpl);

    //全部评价列表
    var wholeUlTpl=$('#whole-ul').html();
    var wholeUlCmp=Handlebars.compile(wholeUlTpl);

    //有图评价
    var chooseImgTpl=$('#chooseimg').html();
    var chooseImgCmp=Handlebars.compile(chooseImgTpl);



    (function getDetail() {
        var spuId=locationSearcher('spuId');

        $.ajax({
            url:C.interface.detail,
            type:'get',
            dataType:'json',
            data:{
                productId:spuId
            },
            success:function (response) {
                if (response.result=='success'){
                    var data=response.data;
                    console.log(data);
                    $('#detail-box').html(detailCmp(data));
                    $('#goods-type').html(goodsTypeCmp(data));

                    //轮播图
                    var slide = new auiSlide({
                        container:document.getElementById("aui-slide"),
                        // "width":300,
                        //"height":$(this).width,
                        "speed":500,
                        "autoPlay": 3000, //自动播放
                        "loop":true,
                        "pageShow":true,
                        "pageStyle":'dot',
                        'dotPosition':'center'
                    });

                    //详情图片
                    $('.goodsdetail-con-detail').html(detailImgCmp(data));
                    var imgWidth=$('.bg-dark').width();

                    $('.aui-slide-wrap').css('height',imgWidth);
                    $('.bg-dark').find('img').css('height',imgWidth);

                    var canPay = data.isSeckill;
                    if (canPay == 0){
                        $('.cart').addClass('gray-cart').removeClass('cart');
                    }

                    //var count=data.skus.length;
                    //console.log(count);
                    var typeList=$('.goods-type-list');
                    if (typeList.length>1){
                        typeList.eq(0).addClass('goods-type-active');

                        typeList.bind('click',function () {
                            $(this).addClass('goods-type-active').siblings().removeClass('goods-type-active');
                        });
                        oneSkuId=$('.goods-type-active').data('skuid');
                    }else {
                        oneSkuId=$('.goods-type-list').data('skuid');
                        $('#goods-type').remove();
                    }
                    //console.log(oneSkuId);
                    $('.cart').bind('click',function () {
                        var buyId = spuId + ':'+oneSkuId+':'+'1';
                        localStorage.setItem('orderSubmitId',buyId);
                        location.href='seckill_pay.html?spuId='+spuId+'&'+'skuId='+oneSkuId;
                    })
                }else if (response.result === 'login'){
                    alert('登录过期请重新登录');
                    againLogin();
                }else {
                    alert(response.errorMsg);
                }
            }
        });

        //评价
        $.ajax({
            url:C.interface.detailFeedbacks,
            type:'POST',
            dataType:'JSON',
            data:{
                productId:spuId,
                isHaveImg:'1'

            },
            success:function (data) {
                if(data.result=='success'){
                    var data=data.data;
                    console.log(data)
                    $('.goodsdetail-con-whole').html(comWholeCmp(data));

                    // <!--所有评价和图片评价切换-->
                    $('.goodsdetail-whole-lr').click(function () {
                        $(this).addClass('goodsdetail-active2').find('span').css('color', 'red');
                        $(this).siblings().removeClass('goodsdetail-active2').find('span').css('color', '#1f1f1f');
                        var index1 = $(this).index();
                        $('.goodsdetail-whole-con>div').eq(index1).show().siblings().hide();

                        $('.whole').html(wholeUlCmp(data));

                        $('.chooseimg').html(chooseImgCmp(data));
                    });
                }
            }
        })
    })();




    
});
